package com.obrempon.credit.market.ui.activity

import android.annotation.SuppressLint
import android.os.CountDownTimer
import android.view.View
import android.view.animation.Animation
import android.view.animation.RotateAnimation
import com.obrempon.credit.market.R
import com.obrempon.credit.market.common.CommonActivity
import com.obrempon.credit.market.databinding.ObremponWaitMsgActBinding
import com.obrempon.credit.market.event.ObremponEventData
import com.obrempon.credit.market.event.ObremponEventPointType
import com.obrempon.credit.market.utils.helper.ObremponProcessHelper
import com.obrempon.credit.market.utils.helper.TestAnalysisAddEvent
import com.obrempon.credit.market.utils.helper.TestDataLibInvoke

/**
 * ObremponCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/25 18:45
 * @Annotation：SMS 等待
 */
class ObremponWaitMSGAct: CommonActivity() {
    lateinit var binding: ObremponWaitMsgActBinding
    //等待时长
    private var countdown = 10
    private var countdownTimer: MyCountDownTimer? = null

    override fun takeObremponRootView(): View {
        binding = ObremponWaitMsgActBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initObremponView() {
        setObremponTitleBarVisibility(View.VISIBLE)
        setObremponTextTitleVisibility(View.GONE)
        setObremponBackBtnVisibility(View.INVISIBLE)
        setObremponRightBtnRes(R.mipmap.obrempon_customer_icon)
        rootViewBinding.rootLayout.setBackgroundColor(getColor(R.color.color_ff0569ff))
    }

    override fun initObremponListener() {
        //联系客服
        rootViewBinding.rootCustomer.setOnClickListener {
            showCustomerDialog("SMS Preview Page")
            addEventPointRequestAction(ObremponEventPointType.CLICK.OBREMPON_CLICK_CUSTOMER,"SMS Preview Page")
        }
    }

    override fun takeObremponData() {
        if (countdownTimer == null){
            countdownTimer = MyCountDownTimer(this, countdown * 1000, 1000)
        }
        countdownTimer?.start()
    }

    class MyCountDownTimer(private val act: ObremponWaitMSGAct, millisInFuture: Int, countDownInterval: Long): CountDownTimer(millisInFuture.toLong(), countDownInterval) {
        override fun onTick(millisUntilFinished: Long) {
            // 这里每隔一段时间（定时器间隔）会被调用
            // 可以在这里更新UI，显示剩余时间
            act.countdown = (millisUntilFinished / 1000).toInt()
            act.showUploadSmsDataTipsText()
        }
        override fun onFinish() {
            // 倒计时完成时被调用
            act.showUploadSmsDataComplete()
        }
    }

    /**
     * 设置文本
     */
    @SuppressLint("SetTextI18n")
    private fun showUploadSmsDataTipsText(){
        binding.textCountdown.text = "$countdown"
        showApplySmsDataStepStatusView()
    }

    /**
     * 倒计时完成
     */
    private fun showUploadSmsDataComplete(){
        showObremponLoading()
        //上报所有设备数据
        publishEventData(ObremponEventData(ObremponEventData.TYPE.OBREMPON_SYC_ALL_DEVICE))
        //刷新主流程
        publishEventData(ObremponEventData(ObremponEventData.TYPE.OBREMPON_PROCESS_REFRESH))
    }

    /**
     *旋转动画
     */
    private fun getApplySmsDataAnimation(): Animation {
        // 创建一个旋转动画对象，从当前的角度旋转到指定的角度，持续时间为1000毫秒
        val rotateAnimation = RotateAnimation(0F, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        rotateAnimation.setDuration(1000L) // 设置动画持续时间
//        rotateAnimation.fillAfter = false // 动画执行完后保持结束状态
        rotateAnimation.isFillEnabled = true;
        rotateAnimation.fillBefore = false;
        rotateAnimation.setRepeatCount(Animation.INFINITE); // 设置动画重复次数
        return rotateAnimation
    }

    /**
     * 显示状态
     */
    private fun showApplySmsDataStepStatusView(){
        if (countdown > 6){
            binding.imgApplyWaitStep1.setImageResource(R.mipmap.obrempon_apply_wait_step2_icon)
            binding.imgApplyWaitStep2.setImageResource(R.mipmap.obrempon_apply_wait_step3_icon)
            binding.imgApplyWaitStep3.setImageResource(R.mipmap.obrempon_apply_wait_step3_icon)
            binding.viewApplyWaitLine1.shapeDrawableBuilder
                .setSolidColor(getColor(R.color.color_ffbbbbbb))
                .intoBackground()
            binding.viewApplyWaitLine2.shapeDrawableBuilder
                .setSolidColor(getColor(R.color.color_ffbbbbbb))
                .intoBackground()
            return
        }

        if (countdown > 3){
            binding.imgApplyWaitStep1.setImageResource(R.mipmap.obrempon_apply_wait_step1_icon)
            binding.imgApplyWaitStep2.setImageResource(R.mipmap.obrempon_apply_wait_step2_icon)
            binding.imgApplyWaitStep3.setImageResource(R.mipmap.obrempon_apply_wait_step3_icon)
            binding.viewApplyWaitLine1.shapeDrawableBuilder
                .setSolidColor(getColor(R.color.color_ff0569ff))
                .intoBackground()
            binding.viewApplyWaitLine2.shapeDrawableBuilder
                .setSolidColor(getColor(R.color.color_ffbbbbbb))
                .intoBackground()
            return
        }
        binding.imgApplyWaitStep1.setImageResource(R.mipmap.obrempon_apply_wait_step1_icon)
        binding.imgApplyWaitStep2.setImageResource(R.mipmap.obrempon_apply_wait_step1_icon)
        binding.viewApplyWaitLine1.shapeDrawableBuilder
            .setSolidColor(getColor(R.color.color_ff0569ff))
            .intoBackground()
        binding.viewApplyWaitLine2.shapeDrawableBuilder
            .setSolidColor(getColor(R.color.color_ff0569ff))
            .intoBackground()

        if (countdown >= 1){
            binding.imgApplyWaitStep3.setImageResource(R.mipmap.obrempon_apply_wait_step2_icon)
            return
        }
        binding.imgApplyWaitStep3.setImageResource(R.mipmap.obrempon_apply_wait_step1_icon)
    }

    override fun onObremponPublishEvent(event: ObremponEventData) {
        super.onObremponPublishEvent(event)
        when(event.eventType){
            ObremponEventData.TYPE.OBREMPON_PROCESS_REFRESHED -> {
                hideObremponLoading()
                //需要校验 WhatsApp 或 SMS
                if (ObremponProcessHelper.isObremponAuthCheckWhatsApp() || ObremponProcessHelper.isObremponAuthCheckSMS()){
//                    CheckAct.start(this)
                } else {
//                    val pName = getProductName()
//                    val pToken = getProductToken()
//                    ActivityOrderDetails.start(this, pName, pToken)
                }
                finish()
            }
            else -> {

            }
        }
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(ObremponEventPointType.PAGE.OBREMPON_PAGE_LOAN_SMS_PREVIEW_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        addEventPointRequestAction(ObremponEventPointType.PAGE.OBREMPON_PAGE_LOAN_SMS_PREVIEW_END)
        if (countdownTimer != null){
            countdownTimer?.cancel()
            countdownTimer = null
        }
    }
}